package kr.wisdomh.wisdom_career;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	private ViewPager mPager;
	private Button btn01;
	private Button btn02;
	private Button btn03;
	private Button btn04;
	private Button btn05;
	private Button btn06;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btn01 = (Button) findViewById(R.id.main_btn01);
		btn02 = (Button) findViewById(R.id.main_btn02);
		btn03 = (Button) findViewById(R.id.main_btn03);
		btn04 = (Button) findViewById(R.id.main_btn04);
		btn05 = (Button) findViewById(R.id.main_btn05);
		btn06 = (Button) findViewById(R.id.main_btn06);
		btn01.setTextColor(Color.RED);

		setLayout();

		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(new PagerAdapterClass(getApplicationContext()));

		mPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				ColorWhite();
				if (position == 0) {
					btn01.setTextColor(Color.RED);
				} else if (position == 1) {
					btn02.setTextColor(Color.RED);
				} else if (position == 2) {
					btn03.setTextColor(Color.RED);
				} else if (position == 3) {
					btn04.setTextColor(Color.RED);
				} else if (position == 4) {
					btn05.setTextColor(Color.RED);
				} else {
					btn06.setTextColor(Color.RED);
				}
			}

			@Override
			public void onPageScrolled(int position, float positionOffest,
					int positionOffsetPixels) {
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
	}

	public void ColorWhite() {
		btn01.setTextColor(Color.WHITE);
		btn02.setTextColor(Color.WHITE);
		btn03.setTextColor(Color.WHITE);
		btn04.setTextColor(Color.WHITE);
		btn05.setTextColor(Color.WHITE);
		btn06.setTextColor(Color.WHITE);
	}
	
	public void mOnClick(View v){
		Intent intent = new Intent();
		switch(v.getId()){
		case R.id.main_addbtn:
			intent.setClass(this, AddInfo.class);
			break;
		}
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			setCurrentInflateItem(0);
			break;
		case R.id.button2:
			setCurrentInflateItem(1);
			break;
		case R.id.button3:
			setCurrentInflateItem(2);
			break;
		case R.id.button4:
			setCurrentInflateItem(3);
			break;
		case R.id.button5:
			setCurrentInflateItem(4);
			break;
		case R.id.button6:
			setCurrentInflateItem(5);
			break;
		}
	}

	private void setCurrentInflateItem(int type) {
		ColorWhite();
		if (type == 0) {
			mPager.setCurrentItem(0);
		} else if (type == 1) {
			mPager.setCurrentItem(1);
		} else if (type == 2) {
			mPager.setCurrentItem(2);
		} else if (type == 3) {
			mPager.setCurrentItem(3);
		} else if (type == 4) {
			mPager.setCurrentItem(4);
		} else {
			mPager.setCurrentItem(5);
		}
	}

	/**
	 * Layout
	 */
	private void setLayout() {
		btn01.setOnClickListener(this);
		btn02.setOnClickListener(this);
		btn03.setOnClickListener(this);
		btn04.setOnClickListener(this);
		btn05.setOnClickListener(this);
		btn06.setOnClickListener(this);
	}

	/**
	 * PagerAdapter
	 */
	private class PagerAdapterClass extends PagerAdapter {
		private LayoutInflater mInflater;

		public PagerAdapterClass(Context c) {
			super();
			mInflater = LayoutInflater.from(c);
		}

		@Override
		public int getCount() {
			return 6;
		}

		@Override
		public Object instantiateItem(View pager, int position) {
			View v = null;
			if (position == 0) {
				v = mInflater.inflate(R.layout.page_first, null);
			} else if (position == 1) {
				v = mInflater.inflate(R.layout.page_second, null);
			} else if (position == 2) {
				v = mInflater.inflate(R.layout.page_third, null);
			} else if (position == 3) {
				v = mInflater.inflate(R.layout.page_fourth, null);
			} else if (position == 4) {
				v = mInflater.inflate(R.layout.page_fifth, null);
			} else {
				v = mInflater.inflate(R.layout.page_sixth, null);
			}

			((ViewPager) pager).addView(v, 0);
			return v;
		}

		@Override
		public void destroyItem(View pager, int position, Object view) {
			((ViewPager) pager).removeView((View) view);
		}

		@Override
		public boolean isViewFromObject(View pager, Object obj) {
			return pager == obj;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		@Override
		public void finishUpdate(View arg0) {
		}
	}

}
